package com.hs.chd.dwr;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import com.hs.core.cache.DictCacheUtil;
import com.hs.core.db.DBMgr;
import com.hs.core.dwr.DwrHelper;
import com.hs.core.utils.TimeUtil;

public class DataHelper  extends DwrHelper{

	/**
	 * 删除文献信息
	 * @param idStr
	 * @return
	 */
	public String deleteCNPaper(String idStr){
		DBMgr inst = this.getDbo();
		String sql = "delete from SCHD_CHNPAPER where id=?";
		System.out.println("id:" + idStr);
		int count =0;
		if(StringUtils.isNotEmpty(idStr)){
			String[] ids = idStr.split(",");
			for(int k=0;k<ids.length;k++){
				int i = inst.update(sql, new Object[]{ids[k]});
				if(i>0)
					count++;
			}
		}
		deleteENPaper(idStr); 
		return count+"";
	}
	/**
	 * 删除英文文献信息
	 * @param idStr
	 * @return
	 */
	public String deleteENPaper(String idStr){
		DBMgr inst = this.getDbo();
		String sql = "delete from SCHD_ENGPAPER where id=?";
		System.out.println("id:" + idStr);
		int count =0;
		if(StringUtils.isNotEmpty(idStr)){
			String[] ids = idStr.split(",");
			for(int k=0;k<ids.length;k++){
				int i = inst.update(sql, new Object[]{ids[k]});
				if(i>0)
					count++;
			}
		}
		return count+"";
	}
	/**
	 * 删除古籍文献信息
	 * @param idStr
	 * @return
	 */
	public String deleteOLDPaper(String idStr){
		DBMgr inst = this.getDbo();
		String sql = "delete from SCHD_OLDPAPER where id=?";
		System.out.println("id:" + idStr);
		int count =0;
		if(StringUtils.isNotEmpty(idStr)){
			String[] ids = idStr.split(",");
			for(int k=0;k<ids.length;k++){
				int i = inst.update(sql, new Object[]{ids[k]});
				if(i>0)
					count++;
			}
		}
		return count+"";
	}
	/**
	 * 删除中文文献原文信息
	 * @param idStr
	 * @return
	 */
	public String deleteCNPaperFile(String fileid){
		DBMgr inst = this.getDbo();
		String sql = "delete from SCHD_FILEINFO where PAPERID=?";
		String sql2 = "update SCHD_CHNPAPER set ISIMG=0 where id=?";
		int count =0;
		if(StringUtils.isNotEmpty(fileid)){
			String[] ids = fileid.split(",");
			for(int k=0;k<ids.length;k++){
				int i = inst.update(sql, new Object[]{ids[k]});
				inst.update(sql2, new Object[]{ids[k]});
				if(i>0)
					count++;
			}
		}
		deleteENPaperFile(fileid);
		return count+"";
	}
	/**
	 * 删除英文文文献原文信息
	 * @param idStr
	 * @return
	 */
	public String deleteENPaperFile(String fileid){
		DBMgr inst = this.getDbo();
		String sql = "delete from SCHD_FILEINFO where PAPERID=?";
		String sql2 = "update SCHD_ENGPAPER set ISIMG=0 where id=?";
		int count =0;
		if(StringUtils.isNotEmpty(fileid)){
			String[] ids = fileid.split(",");
			for(int k=0;k<ids.length;k++){
				int i = inst.update(sql, new Object[]{ids[k]});
				inst.update(sql2, new Object[]{ids[k]});
				if(i>0)
					count++;
			}
		}
		return count+"";
	}
	/**
	 * 删除古籍文献原文信息
	 * @param idStr
	 * @return
	 */
	public String deleteOLDPaperFile(String fileid){
		DBMgr inst = this.getDbo();
		String sql = "delete from SCHD_FILEINFO where PAPERID=?";
		String sql2 = "update SCHD_OLDPAPER set ISIMG=0 where id=?";
		int count =0;
		if(StringUtils.isNotEmpty(fileid)){
			String[] ids = fileid.split(",");
			for(int k=0;k<ids.length;k++){
				int i = inst.update(sql, new Object[]{ids[k]});
				inst.update(sql2, new Object[]{ids[k]});
				if(i>0)
					count++;
			}
		}
		return count+"";
	}
	/**
	 * 删除标准数据
	 * @param idStr
	 * @return
	 */
	public String deleteSta(String idStr){
		DBMgr inst = this.getDbo();
		String sql = "delete from CHD_STANDWORD where id=?";
		int count =0;
		if(StringUtils.isNotEmpty(idStr)){
			String[] ids = idStr.split(",");
			for(int k=0;k<ids.length;k++){
				int i = inst.update(sql, new Object[]{ids[k]});
				if(i>0)
					count++;
			}
		}
		return count+""; 
	}
	
	/**
	 * 增加主题分类
	 * @param tagname
	 * @param pid
	 * @param userid
	 * @param tagtype
	 * @return
	 */
	public String addTag(String tagname,String pid,String userid,String tagtype){
		String seqid = this.getSeqValue("SEQ_CHD_TAGS_ID");
		
		String sql = "insert into CHD_TAGS(ID, PID, TAGNAME, TAGCODE, IMPDATE, ISDELETE,TAGTYPE, ISROOT,USERID) VALUES(?,?,?,?,?,?,?,?,?)";
      	int count =	this.getDbo().update(sql, new Object[]{seqid,pid,tagname,tagname,TimeUtil.getCurrentstr1(),1,tagtype,seqid,userid});
      	
      	if(count > 0){
      		return seqid+'#' + tagname;
      	}
		return "";
	}
    /**
     * 删除主题分类
     * @param tagid
     * @param userid
     * @return
     */
	public String delTag(String tagid,String userid){
		String sql = "update CHD_TAGS set isdelete=?,userid=? where id=?" ;
		int  count = this.getDbo().update(sql, new Object[]{0,userid,tagid});
		return ""+count;
	}
	/**
	 * 更新主题分类
	 * @param tagid
	 * @param tagname
	 * @param userid
	 * @return
	 */
	public String updateTag(String tagid,String tagname,String oldname,String userid){
		String tagnamecn = tagname;
		String tagnameen = null;
		if(null != tagname && tagname.indexOf("$")!=-1){
      		int index = tagname.indexOf("$");
      		tagnamecn = tagname.substring(0,index);
      		tagnameen = tagname.substring(index+1);
      		System.out.println("tagnamecn:"+tagnamecn);
      		System.out.println("tagnameen:"+tagnameen);

      	}
		String sql = "update CHD_TAGS set TAGNAME=?, TAGCODE=?,USERID=? where id=?" ;
		String sql1 = "select * from CHD_TAGS where tagname=? and isdelete=1 and　pid=(select pid from CHD_TAGS where id=?)";
		List datalist = this.getDbo().queryList(sql1, new Object[]{tagnamecn,tagid});
		String returnmessaqe = "";
		if(datalist.size() == 0){
			int  count = this.getDbo().update(sql, new Object[]{tagnamecn,tagname,userid,tagid});
			if(null != tagnameen){
				
				try {
					this.getDbo().update("insert into CHD_SUBJECTCODE(NAME_CN,NAME_EN) values(?,?)", new Object[]{tagnamecn,tagnameen});
					DictCacheUtil dictutil = DictCacheUtil.getInst();
					dictutil.addSingleSubjectcode(tagnamecn,tagnameen);
				} catch (RuntimeException e) { 
					System.out.println("exception message:" + e.getMessage());
				}
			}
 		}else{
			returnmessaqe = oldname+"#" + tagid;
		}
		return returnmessaqe; 
	}
}
